﻿Imports System.IO
Imports sndir.Directory
Imports sndir.Connection
Imports System.ComponentModel

Public Class Form1

#Region "Configuration"
    Const Root As String = "C:\SirenNinja\Backuper"
    Const RootBack As String = Root + "\Backup"
    Dim _textone As String = ""
    Dim _texttwo As String = ""
    Public Shared Connecter As String = "N/A"
#End Region

#Region "Connection/Loading"
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        BackgroundWorker1.RunWorkerAsync()
        End Sub

    Private Sub ConnectionBar_Click(sender As Object, e As EventArgs) Handles ConnectionBar.Click
        Try
            Dim nameFolder As String = InputBox("Type a website. (Ex. www.google.com).")
            Try
                Connection(nameFolder)
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        If Not My.Computer.FileSystem.FileExists(Application.StartupPath + "\sndir.dll") Then
            MsgBox("sncd.dll is missing. Please redownload the program!")
            Application.Exit()
        End If
    End Sub

    Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
        snCreateDirectory(RootBack)
        Connection("www.google.com")
    End Sub

#End Region

#Region "Timer(s)"
    Private Sub Constant_Tick(sender As Object, e As EventArgs) Handles Constant.Tick

        If My.User.IsInRole(ApplicationServices.BuiltInRole.Administrator) Then
            LogInThemeContainer1.Text = "Backuper (Administrator) [" + Height.ToString + " , " + Width.ToString + "]"
        Else
            LogInThemeContainer1.Text = "Backuper (User) [" + Height.ToString + " , " + Width.ToString + "]"
        End If

        'Folder and file
        If files.Checked = True Then
            sfFolder.Enabled = False
            sfFile.Enabled = True
        Else
            sfFile.Enabled = False
            sfFolder.Enabled = True
        End If

        'Custom and program directory
        If backupdirectory.Checked = True Then
            sbfFolder.Enabled = False
            sbfDirectory.Enabled = False
            sbfText.Text = RootBack
        Else
            sbfFolder.Enabled = True
            sbfDirectory.Enabled = True
        End If

        'Start button
        If backupdirectory.Checked = True Then
            If Not sfText.Text = Nothing Then
                Start.Enabled = True
            Else
                Start.Enabled = False
            End If
        Else
            If Not sfText.Text = Nothing And sbfFolder.Enabled = False Then
                Start.Enabled = True
            Else
                Start.Enabled = False
            End If

            If Not sfText.Text = Nothing And Not sbfText.Text = Nothing Then
                Start.Enabled = True
            Else
                Start.Enabled = False
            End If
        End If

        ConnectionBar.Text = Connecter
    End Sub
#End Region

#Region "Buttons 1/2/3"
    Private Sub sfFolder_Click(sender As Object, e As EventArgs) Handles sfFolder.Click
        If FolderBrowserDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            FolderBrowserDialog1.ShowNewFolderButton = True
            sfText.Text = FolderBrowserDialog1.SelectedPath
        End If


    End Sub

    Private Sub sfFile_Click(sender As Object, e As EventArgs) Handles sfFile.Click
        Dim ofd As New OpenFileDialog
        ofd.Title = "Open file"
        ofd.Multiselect = False
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            sfText.Text = ofd.FileName
        End If
        'MsgBox()
    End Sub

    Private Sub sbfFolder_Click(sender As Object, e As EventArgs) Handles sbfFolder.Click
        If FolderBrowserDialog2.ShowDialog = Windows.Forms.DialogResult.OK Then
            FolderBrowserDialog2.ShowNewFolderButton = True
            sbfText.Text = FolderBrowserDialog2.SelectedPath
        End If
    End Sub
#End Region

#Region "Gathering the files/creating stuff..... Why have I not put this in a dll yet? -_-. Oh well.."
    Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click


        'Program Directory
        If backupdirectory.Checked = True Then
            _texttwo = Root + "\Backup"

            'Folder
            If folder.Checked = True Then
                sbfText.Text = Root + "\Backup"
                Try
                    Dim dir1 As DirectoryInfo = New DirectoryInfo(sfText.Text)
                    If dir1.Exists Then
                        _textone = sfText.Text
                    Else
                        MsgBox(sfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If
                Catch ex As Exception

                End Try

                'File
            ElseIf files.Checked = True Then
                sbfText.Text = Root + "\Backup"
                Try
                    Dim dir2 As FileInfo = New FileInfo(sfText.Text)
                    If dir2.Exists Then
                        _textone = sfText.Text
                    Else
                        MsgBox(sfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If
                Catch ex As Exception

                End Try

                'Error
            Else
                MsgBox("Nothing checked in settings?")
            End If









            'Custom Directory
        ElseIf customdirectory.Checked = True Then


            'Folder
            If folder.Checked = True Then
                Try
                    Dim dir3 As DirectoryInfo = New DirectoryInfo(sfText.Text)
                    If dir3.Exists Then
                        _textone = sfText.Text
                    Else
                        MsgBox(sfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If

                    Dim dir4 As DirectoryInfo = New DirectoryInfo(sbfText.Text)
                    If dir4.Exists Then
                        _textone = sbfText.Text
                    Else
                        MsgBox(sbfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If
                Catch ex As Exception

                End Try

                'File
            ElseIf files.Checked = True Then
                Try
                    Dim dir5 As FileInfo = New FileInfo(sfText.Text)
                    If dir5.Exists Then
                        _textone = sfText.Text
                    Else
                        MsgBox(sfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If

                    Dim dir6 As DirectoryInfo = New DirectoryInfo(sbfText.Text)
                    If dir6.Exists Then
                        _textone = sbfText.Text
                    Else
                        MsgBox(sbfText.Text + " doesn't exist!", MsgBoxStyle.Critical, "Error")
                        Return
                    End If
                Catch ex As Exception

                End Try


                'Error
            Else
                MsgBox("Nothing checked in settings?")
                Return
            End If


            'Error
        Else
            MsgBox("Nothing checked in settings?")
            Return
        End If






        If _textone = Nothing And _texttwo = Nothing Then
        Else
            LogInLabel3.Visible = True
            Finish()
        End If
    End Sub

    Private Sub Finish()
        Try
            If LogInOnOffSwitch2.Toggled = LogInOnOffSwitch.Toggles.Toggled Then
                Static nameFolder As String = InputBox("Please select a name to put the files in.")
                FolderAfterStart(nameFolder)
            Else
                NoFolderAfterStart()
                If Not sbfText.Text = Root + "\Backup" Then
                    My.Settings.form2.Add(sbfText.Text)
                End If
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub FolderAfterStart(destFolder As String)
        Static processing As String
        If folder.Checked = True Then
            If customdirectory.Checked = True Then
                My.Computer.FileSystem.CopyDirectory(sfText.Text, sbfText.Text + "\" + destFolder)
                checkWithName(sbfText.Text + "\" + destFolder, False)
                processing = sbfText.Text + "\" + destFolder
            Else
                My.Computer.FileSystem.CopyDirectory(sfText.Text, RootBack + "\" + destFolder)
                checkWithName(RootBack + "\" + destFolder, False)
                processing = RootBack + "\" + destFolder
            End If

            If LogInOnOffSwitch3.Toggled = LogInOnOffSwitch.Toggles.Toggled Then
                Process.Start(processing)
            End If
            LogInLabel3.Visible = False
        End If



        If files.Checked = True Then
            If customdirectory.Checked = True Then
                My.Computer.FileSystem.CopyFile(sfText.Text, sbfText.Text + "\" + destFolder + "\" + Path.GetFileName(sfText.Text))
                checkWithName(sbfText.Text + "\" + destFolder + "\" + Path.GetFileName(sfText.Text), True)
                processing = sbfText.Text + "\" + destFolder
            Else
                My.Computer.FileSystem.CopyFile(sfText.Text, RootBack + "\" + destFolder + "\" + Path.GetFileName(sfText.Text))
                checkWithName(RootBack + "\" + destFolder + "\" + Path.GetFileName(sfText.Text), True)
                processing = RootBack + "\" + destFolder
            End If


            If LogInOnOffSwitch3.Toggled = LogInOnOffSwitch.Toggles.Toggled Then
                Process.Start(processing)
            End If
            LogInLabel3.Visible = False
        End If

        If Not sbfText.Text = RootBack Then
            My.Settings.form2.Add(sbfText.Text + "\" + destFolder)
        End If
    End Sub

    Private Sub NoFolderAfterStart()
        Static processing As String
        If folder.Checked = True Then
            If customdirectory.Checked = True Then
                My.Computer.FileSystem.CopyDirectory(sfText.Text, sbfText.Text)
                checkWithoutName(sbfText.Text, False)
                processing = sbfText.Text
            Else
                My.Computer.FileSystem.CopyDirectory(sfText.Text, RootBack)
                checkWithoutName(RootBack, False)
                processing = RootBack
            End If

            If LogInOnOffSwitch3.Toggled = LogInOnOffSwitch.Toggles.Toggled Then
                Process.Start(processing)
            End If
            LogInLabel3.Visible = False
        End If



        If files.Checked = True Then
            If customdirectory.Checked = True Then
                My.Computer.FileSystem.CopyFile(sfText.Text, sbfText.Text + "\" + Path.GetFileName(sfText.Text))
                checkWithoutName(sbfText.Text + "\" + Path.GetFileName(sfText.Text), True)
                processing = sbfText.Text
            Else
                My.Computer.FileSystem.CopyFile(sfText.Text, sbfText.Text + "\" + Path.GetFileName(sfText.Text))
                checkWithoutName(RootBack + "\" + Path.GetFileName(sfText.Text), True)
                processing = RootBack
            End If

            If LogInOnOffSwitch3.Toggled = LogInOnOffSwitch.Toggles.Toggled Then
                Process.Start(processing)
            End If
            LogInLabel3.Visible = False
        End If
    End Sub

    Private Sub checkWithoutName(dest As String, file As Boolean)

        Try
            If file = False Then
                Dim dir As DirectoryInfo = New DirectoryInfo(dest)

                If dir.Exists Then
                    MsgBox("The folder has been backed up!")
                Else
                    MsgBox("Error: The folder wasn't backed up!")
                End If
            Else
                Dim fil As FileInfo = New FileInfo(dest)

                If fil.Exists Then
                    MsgBox("The file has been backed up!")
                Else
                    MsgBox("Error: The file wasn't backed up!")
                End If
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub checkWithName(dest As String, file As Boolean)

        Try
            If file = False Then
                Dim dir As DirectoryInfo = New DirectoryInfo(dest)

                If dir.Exists Then
                    MsgBox("The folder has been backed up!")
                Else
                    MsgBox("Error: The folder wasn't backed up!")
                End If
            Else
                Dim fil As FileInfo = New FileInfo(dest)

                If fil.Exists Then
                    MsgBox("The file has been backed up!")
                Else
                    MsgBox("Error: The file wasn't backed up!")
                End If
            End If
        Catch ex As Exception

        End Try

    End Sub
#End Region

#Region "TextBox changes and stuff"
    Private Sub LogInRadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles folder.Click
        sfText.Text = ""
    End Sub

    Private Sub LogInRadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles files.Click
        sfText.Text = ""
    End Sub

    Private Sub LogInButton5_Click(sender As Object, e As EventArgs) Handles sbfDirectory.Click
        Form2.Show()
    End Sub

    Private Sub LogInRadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles customdirectory.Click
        sbfText.Text = ""
    End Sub
#End Region

End Class
